﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.FrameworkSample (SharePoint 2010/V4)
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends! 
//_______________________________________________________________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sapiens.at.SharePoint.Entities;
using Microsoft.SharePoint;
using Sapiens.at.SharePoint.Utils;
using QB = Sapiens.at.SharePoint.Utils.QueryBuilder;
using Sapiens.at.SharePoint.Repositories;

namespace Sapiens.at.FrameworkSample.Entities
{
    [ContentType(CTYPE_ID)] // mark the class with its content type
    public class Department : Item
    {
        #region Constants
        public new const string CTYPE_ID = "0x01000f643cab80bf4ff182ed580be49a9354";
        public new static readonly SPContentTypeId CTypeId = new SPContentTypeId(CTYPE_ID);
        public const string ListUrl = "Lists/Departments"; // frequently needed to find the employees list

        public const string EMPLOYEE_COUNT = "fEmployeeCount";
        public const string IS_MANAGEMENT = "fIsManagement";
        #endregion

        #region Properties
        public int EmployeeCount
        {
            get { return Get<int>(EMPLOYEE_COUNT); }
            set { Set(EMPLOYEE_COUNT, value); }
        }

        public bool IsManagement
        {
            get { return Get<bool>(IS_MANAGEMENT); }
            set { Set(IS_MANAGEMENT, value); }
        }

        public IEnumerable<Employee> Employees
        {
            get { return GetInverseLookups<Employee>(Employee.ListUrl, Employee.LOOKUP_DEPARTMENT); }
        }
        #endregion

        #region Constructor
        public Department(int id, string title) : base(id, title) { }
        public Department(int id, string title, SPList list) : base(id, title, list) { }
        public Department(SPItemEventProperties eventProperties, SPWeb web) : base(eventProperties, web) { }
        public Department(SPListItem listItem) : base(listItem) { }
        public Department(SPItemEventProperties eventProperties, SPListItem listItem, SPWeb web) : base(eventProperties, listItem, web) { }
        #endregion

        #region Methods
        public void DecreaseEmployeeCount()
        {
            EmployeeCount--;
            if (EmployeeCount < 0)
            {
                EmployeeCount = 0;
            }
            SystemUpdate();
        }

        public void IncreaseEmployeeCount()
        {
            EmployeeCount++;
            SystemUpdate();
        }

        public static IEnumerable<Department> GetNonEmptyManagementDeps(SPWeb web)
        {
            var repository = new ListRepository(Department.ListUrl, web);
            var query = new SPQuery()
                .Where(
                    QB.And(
                        QB.Gt(Department.EMPLOYEE_COUNT, 0),
                        QB.Eq(Department.IS_MANAGEMENT, true)));

            return repository.GetEntities<Department>(query);
        }

        #endregion
    }
}
